<?php include './header.php';


if (isset($_GET['case_id'])) {
    $id = $_GET['case_id'];
    $select_condition = "INNER JOIN nnd_case_type WHERE case_id = $id";
    $case = $db -> selectAll('cases', '*', $select_condition)[0];
}
//pre($case);

if(isset($_POST['edit'])){
    $update_condition = "WHERE case_id = $id";
    $value = '';
    if($_FILES['pic1']['error'] == 0){
        $old_img = $_POST['old_img'];
        $old_thumb = $_POST['old_thumb'];
        $case_img_mark = $_POST['old_img_mark'];
        $case_str_mark = $_POST['old_str_mark'];

        if(is_file($old_img)){
            unlink($old_img);
        }
        if(is_file($old_thumb)){
            unlink($old_thumb);
        }
        if(is_file($case_img_mark)){
            unlink($case_img_mark);
        }
        if(is_file($case_str_mark)){
            unlink($case_str_mark);
        }

        $pic_conf = [
            'table' => 'cases',
            'name' => 'pic1',
            'prefix' => 'nnd_'
        ];
        $up = Upload::getInstance($pic_conf);
        $up_img = $up -> downLoad();
        if($up_img['code'] == 1){
            $case_img = $up_img['path'];
            $dir = $up_img['dir'];
            $name = $up_img['new_name'];
            //缩略图参数
            $thumb_conf = [
                'img_addr' => $case_img,
                'path' => $dir,
                'name' => $name,
                'width' => 150,
                'height' => 102
            ];
            $th = Thumb::getInstance($thumb_conf);
            $thumb = $th->download();

            // 文字水印参数
            $str_conf = [
                'fonts' => 'fonts/STXINGKA.TTF',
                'str' => '哈哈哈',
                'img_addr' => $case_img,
                'img_name' => $name,
                'dir' => $dir
            ];
            $strmark = StrMark::getInstance($str_conf);
            $str_mark = $strmark->make();

            // 图片水印参数
            $img_conf = [
                'ori_img' => $case_img,
                'water_img' => 'upload/logo.png',
                'name' => $name,
                'dir' => $dir
            ];
            $imgmark = ImgMark::getInstance($img_conf);
            $img_mark = $imgmark->make();

            $value .= '`case_img`="'.$case_img.'",';
            $value .= '`case_thumb`="'.$thumb.'",';
            $value .= '`case_str_mark`="'.$str_mark.'",';
            $value .= '`case_img_mark`="'.$img_mark.'",';
        }
    }

    foreach($_POST as $key => $val){
        if($val != '' && $val != '提交' && $key != 'old_img' && $key != 'old_thumb' && $key != 'old_img_mark' && $key != 'old_str_mark'){
            $value .= '`'.$key.'`'."='".$val."',";
        }
    }

    $time = time();
    $value = $value."`case_time` = '$time'";
    if($db -> update('cases', $value, $update_condition)){
        echo "<script>alert('修改成功！');window.location.href='cases.php'</script>";
    }else{
        echo "<script>alert('修改失败！');window.reload;</script>";
    }

}



?>
<!-- Start: Content -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-fileinput.css" rel="stylesheet">
<section id="content">
    <div id="topbar" class="affix">
        <ol class="breadcrumb">
            <li><a href="#"><span class="glyphicon glyphicon-home"></span></a></li>
            <li class="active">修改资讯</li>
        </ol>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-md-10 col-lg-8 center-column">
                <form action="" method="post" class="cmxform" enctype="multipart/form-data">
                    <div class="panel">
                        <div class="panel-heading">
                            <div class="panel-title">编辑文章</div>
                            <div class="panel-btns pull-right margin-left">
                                <a href="#" onclick="window.history.back();" class="btn btn-default btn-gradient dropdown-toggle"><span class="glyphicon glyphicon-chevron-left"></span></a>
                            </div>
                        </div>
                        <div class="panel-body">
                            <div class="col-md-7">
                                <div class="form-group">
                                    <div class="input-group"><span class="input-group-addon">分类</span>
                                        <select name="case_type" id="standard-list1" class="form-control">
                                            <?php foreach($case_type as $val){ ?>
                                                <option value="<?php echo $val['case_type_id']; ?>" <?php if($case['case_type'] == $val['case_type_id']){echo 'selected="selected"';} ?>><?php echo $val['case_type_name1']; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-group"><span class="input-group-addon">标题</span>
                                        <input type="text" name="case_name" value="<?php echo $case['case_name']; ?>" class="form-control">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <input type="hidden" name="old_img_mark" value="<?php echo $case['case_img_mark']; ?>">
                                    <input type="hidden" name="old_str_mark" value="<?php echo $case['case_str_mark']; ?>">
                                    <input type="hidden" name="old_img" value="<?php echo $case['case_img']; ?>">
                                    <input type="hidden" name="old_thumb" value="<?php echo $case['case_thumb']; ?>">
                                    <div class="fileinput fileinput-new" data-provides="fileinput" id="exampleInputUpload">
                                        <div class="fileinput-new thumbnail" style="width: 200px;height: auto;max-height:150px;">
                                            <img id='picImg' style="width: 100%;height: auto;max-height: 140px;" src="<?php echo ROOT.'/Admin/'.$case['case_img']; ?>" alt="" />
                                        </div>
                                        <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
                                        <div>
                                            <span class="btn btn-primary btn-file">
                                                <span class="fileinput-new">更换图片</span>
                                                <span class="fileinput-exists">换一张</span>
                                                <input type="file" name="pic1" id="picID" accept="image/gif,image/jpeg,image/x-png" />
                                            </span>
                                            <a href="javascript:;" class="btn btn-warning fileinput-exists" data-dismiss="fileinput">移除</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group col-md-12">
                                <script type="text/plain" id="myEditor" name="case_desc" style="width:100%;height:200px;"><?php echo $case['case_desc']; ?></script>
                            </div>
                            <div class="col-md-7">
                                <div class="form-group">
                                    <input type="submit" value="提交" name="edit" class="submit btn btn-blue">
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
</section>
<!-- End: Content -->
</div>
<!-- End: Main -->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap-fileinput.js"></script>
<link type="text/css" rel="stylesheet" href="umeditor/themes/default/_css/umeditor.css">
<script src="umeditor/umeditor.config.js" type="text/javascript"></script>
<script src="umeditor/editor_api.js" type="text/javascript"></script>
<script src="umeditor/lang/zh-cn/zh-cn.js" type="text/javascript"></script>
<script type="text/javascript">
    var ue = UM.getEditor('myEditor', {
        autoClearinitialContent: false,
        wordCount: false,
        elementPathEnabled: false,
        initialFrameHeight: 300
    });

    $(function () {
        //比较简洁，细节可自行完善
        $('#uploadSubmit').click(function () {
            var data = new FormData($('#uploadForm')[0]);
            $.ajax({
                url: 'xxx/xxx',
                type: 'POST',
                data: data,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    console.log(data);
                    if(data.status){
                        console.log('upload success');
                    }else{
                        console.log(data.message);
                    }
                },
                error: function (data) {
                    console.log(data.status);
                }
            });
        });

    })
</script>
</body>

</html>